package com.tianchi.lexiang.background.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.util.Log;

public class HddDataBaseVersion {
	
	private static final String TAG = "HddDataBaseVersion";
	private static final String TEXT_PATH = "hdd1/database_version.txt";
	
	public static String getCurrentHddDataBaseVersion(){
		String version = "";
		try {
			String encoding = "GBK";
			File file = new File(TEXT_PATH);
			if (file.isFile() && file.exists()) {
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTXT = null;
				while ((lineTXT = bufferedReader.readLine()) != null) {
					version = lineTXT;
				}
				bufferedReader.close();
				read.close();
			}else{
				version = "0-0";
			}
		} catch (Exception e) {
			Log.d(TAG, "[getCurrentHddDataBaseVersion].....e===>" + e.toString());
		}
		return version.trim();
	}
	
	public static void setCurrentHddDataBaseVersion(){
		try {
			 String version = getCurrentHddDataBaseVersion().trim();
			 Log.d(TAG, "[setCurrentHddDataBaseVersion]....version===>" + version);
			 String numbers[] = version.split("-");
			 version = numbers[0] + "-" + (Integer.parseInt(numbers[1]) + 1);
			 Log.d(TAG, "[setCurrentHddDataBaseVersion]....version===>" + version);
		     File file = new File(TEXT_PATH);
		     FileOutputStream fos = new FileOutputStream(file);
		     OutputStreamWriter osw = new OutputStreamWriter(fos);
		     BufferedWriter bw = new BufferedWriter(osw);
		     
		     bw.write(version);
		     bw.newLine();
		     bw.flush();
		     bw.close();
		     osw.close();
		     fos.close();
		    }
		    catch (FileNotFoundException e1) {
		    	e1.printStackTrace();
		    	Log.d(TAG, "[setCurrentHddDataBaseVersion].....e1====>" + e1.toString()); 
		    } catch (IOException e2) {
		    	e2.printStackTrace();
		    	Log.d(TAG, "[setCurrentHddDataBaseVersion].....e2====>" + e2.toString());
		    }
	}
}
